﻿/*ajax异步调用。
说明：ajax("请求地址","执行成功函数","执行失败函数","get/post");
示例：ajax("u.ashx",function(){alert("yes");},function(){alert("no");},"get");
*/
function ajax(u,s,e,m)
{
    var _r = false;
    try
    {
        _r = new XMLHttpRequest();
    }
    catch (y)
    {
        try
        {
            _r = new ActiveXObject("msxml2.xmlhttp");
        }
        catch (z)
        {
            _r = new ActiveXObject("microsoft.xmlhttp")
        }
    }
    if (!_r) { alert("XmlHttpRequest初始化错误"); return; }

    
    u = u || "";
    m = m || "get";
    _r.open(m, u, true);//参数顺序为 类型，请求地址，是否异步
    
    //获取执行的状态。readyState有0：尚未发送send，1：正在发送请求，2：已经接收内容，3：正在解析内容，4：解析完毕，可以调用
    _r.onreadystatechange = function ()
    {
        if (_r.readyState == 4)
        {//如果已经加载完成
            if (_r.status == 200)
            {
                s(_r.responseText);
            }
            else
            {
                e(_r.status);
            }
        }
    };   //onreadystatechange  end

    _r.send(null);
}


/*
页面dom加载完毕执行的方法。
说明：ready(加载完毕要执行的函数)
示例：ready(function(){alert("dom");});
*/
function ready(f)
{
    ready.bindReady();
    if (ready.isReady)
    {
        f();
    }
    else
    {
        ready.readyList.push(f);
    }
}

ready.isReady = false;
ready.readbBound = false;
ready.readyList = [];

ready.execute = function()
{
    if (!ready.isReady)
    {
        if (!document.body)
        {
            return setTimeout(ready.execute, 16);
        }

        ready.isReady = true;
        if (ready.readyList)
        {
            var len = ready.readyList.length;
            for (n = 0; n < len; n++)
            {
                ready.readyList[n]();
            }
        }
        ready.readyList = null;
    }
}

ready.bindReady = function()
{
    if (ready.readyBound) { return; }
    ready.readbBound = true;

    if (document.readyState === "complete")
    {
        return ready.execute();
    }

    if (! +"\v1")//判断是否为IE
    {
        document.attachEvent("onreadystatechange", function(e)
        {
            if (document.readyState == "complete")
            {
                document.detachEvent("onreadystatechange", arguments.callee);
                ready.execute();
            }
        });

        if (ready.isReady)
        {
            return;
        }

        if (window == top)
        {
            try
            {
                document.documentElement.doScroll("left");
            }
            catch (e)
            {
                setTimeout(arguments.callee, 64);
                return;
            }
        }

        ready.execute();
    }
    else
    {
        document.addEventListener("DOMContentLoaded", function()
        {
            document.removeEventListener("DOMContentLoaded", arguments.callee, false);
            ready.execute();
        }, false);
    }
}

/*
判断是否是IE。true表明是。
*/
function isIE()
{
    return ! +"\v1";
}

/*
获取url地址传递的参数
说明：query(要获取的键,是否解码)
示例：query("id",true);
*/
function query(k,au)
{
    au = au || true;
    var reg = new RegExp("(^|&)"+k+"=([^&]*)(&|$)","i");
    var r = window.location.search.substr(1).match(reg);
    if (r)
    {
        if (au&&(au==true))
        {
            return unescape(r[2]);
        }
        else
        {
            return r[2];
        }
    }
    return null;
}

/*
根据ID获取dom
说明:$(编号)
示例:$("menu");
*/
function $(d)
{
    return document.getElementById(d);
}


function mask(maskID)
{
    //隐藏滚动条
    document.body.style.overflow = "hidden";
    if (document.getElementById(maskID))
    {
        document.getElementById(maskID).style.display = "";
    }
    else
    {
        //不存在该遮罩层
        if (isIE())
        {
            //如果是IE，就创建iframe以遮住dropdownlist
            var d = document.createElement("iframe");
            d.id = maskID;
            with (d.style)
            {
                position = "absolute";
                width = (document.body.scrollWidth+20) + "px";
                height = document.body.scrollHeight+"px";
                top = "0px";
                left = "0px";
                zIndex = "90";
                filter = "Alpha(opacity=50)";
            }
            document.body.appendChild(d);
            d.contentWindow.document.write("<html><head></head><body style=\"background:#666\"></body></html>");
            d.contentWindow.document.close();
        }
        else
        {
            var d = document.createElement("div");
            d.id = maskID;
            with (d.style)
            {
                width = (document.body.scrollWidth + 20) + "px";
                height = document.body.scrollHeight + "px";
                position = "absolute";
                top = "0px";
                left = "0px";
                zIndex = "90";
                backgroundColor = "#666";
                opacity = 0.5; 
            }
            document.body.appendChild(d);
        }//else end
        
       
    }
}

/*
关闭遮罩层
说明：maskClose(名称);
示例：maskClose("mask");
*/
function maskClose(maskID)
{
    document.getElementById(maskID).style.display = "none";
    document.body.style.overflow = "auto";
}

/*
创建一个位于遮罩上的层
说明：_win_(内容,宽度，高度)
示例：_win_("这里是内容",400,300);
*/
function _win_(content,w,h)
{
    var html = "<div style=\"text-align:right;background:#509B02;height:20px;line-height:20px\"><span style=\"cursor:pointer;\" onclick=\"javascript:maskClose('mask');_win_close();\">关闭</span></div><div>内容</div>";

    var o=document.getElementById("_win_");
    if (o)
    {//如果存在对话框，就显示，否则创建该对话框
        o.style.display = "";
        o.innerHTML = html;
        return;
    }

    var d = document.createElement("div");
    d.id = "_win_";
    //计算top、left
    w = w || 400;
    h = h || 300;
    with (d.style)
    {
        position = "absolute";
        width = w+"px";
        height = h+"px";
        border = "2px solid #333";
        backgroundColor = "#fff";
        zIndex = "99";
    }
    
    d.innerHTML = html;
    document.body.appendChild(d);
    //调整位置
    _win_resize(d.id);
    //注册事件
    window.onresize = function () { if (document.getElementById("_win_").style.dispaly != "none") { _win_resize(d.id); } }
    window.onscroll = function ()
    {
        if (document.getElementById("_win_").style.dispaly != "none") { _win_resize(d.id); }
    }
}

/*
模拟窗体调整到屏幕居中位置
说明：_win_resize(编号)
示例：_win_resize("_win_");
*/
function _win_resize(id)
{
    var d = document.getElementById(id);

    //对于scrollTop，safari有自己的方法，但对于滚动条在最顶端，IE下是0||undefined,此时会得到undefined，所以pageYOffset 要放在前面。
    var _top = window.pageYOffset || document.documentElement.scrollTop;
    
    d.style.top = ((document.documentElement.clientHeight - parseInt(d.style.height)) / 2+_top)+"px";
    d.style.left = ((document.documentElement.clientWidth - parseInt(d.style.width)) / 2 + document.documentElement.scrollLeft) + "px";
}

function _win_close()
{
    document.getElementById("_win_").style.display = "none";
}

